package com.dqp.cslggroup.Countdown;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.UI.EventAdapter;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.CompressedBitmap;
import com.dqp.cslggroup.Util.GetTimeUtil;
import com.dqp.cslggroup.Util.UiUtils;
import com.dqp.cslggroup.bean.event;
import com.dqp.cslggroup.greendao.eventDao;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class time extends BaseActivity {
    private ListView recyclerView;
    private TextView tv_name, tv_count, tv_date, tv_bz, tv_plus, tv_tian;
    private ImageView iv_bg;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_view);
        Toolbar toolbar = findViewById(R.id.time_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        iniCamp();
        initList();
        initView();
    }


    public void iniCamp() {
        AppCompatTextView add = findViewById(R.id.add_event);
        recyclerView = findViewById(R.id.rv_events);
        tv_count = findViewById(R.id.tv_days);
        tv_name = findViewById(R.id.tv_events);
        tv_date = findViewById(R.id.tv_start_time);
        tv_bz = findViewById(R.id.tv_msg_bz);
        iv_bg = findViewById(R.id.iv_fav_bg);
        tv_plus = findViewById(R.id.tv_plus);
        tv_tian = findViewById(R.id.tv_tian);
        add.setOnClickListener(v -> UiUtils.startActivityForResult(time.this, add_event.class, 1));
    }

    @SuppressLint("SetTextI18n")
    private void initList() {
        List<event> eventList = MyApplication.getDaoInstant().getEventDao().loadAll();
        if (eventList.size() > 0) {
            EventAdapter eventAdapter = new EventAdapter(eventList, this);
            recyclerView.setAdapter(eventAdapter);
            recyclerView.setOnItemClickListener((adapterView, view, i, l) -> {
                event event = (com.dqp.cslggroup.bean.event) eventAdapter.getItem(i);
                Intent intent = new Intent(time.this, add_event.class);
                intent.putExtra("action", 1);
                intent.putExtra("event", event);
                startActivity(intent);
            });
        } else {
            tv_tian.setVisibility(View.GONE);
            tv_plus.setVisibility(View.GONE);
        }
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        List<event> eventList = MyApplication.getDaoInstant().getEventDao().queryBuilder().where(eventDao.Properties.Zd.eq(1)).build().list();
        if (eventList.size() == 0)
            eventList = MyApplication.getDaoInstant().getEventDao().loadAll();
        if (eventList.size() > 0 && eventList.get(0).getName() != null) {
            Date d = new Date();
            @SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String Dates = df.format(d);
            String name = eventList.get(0).getName();
            String bz = eventList.get(0).getBz();
            String date = eventList.get(0).getDate();
            int zd = eventList.get(0).getZd();
            int sel = eventList.get(0).getSelect();
            String bg = eventList.get(0).getBg();
            tv_tian.setVisibility(View.VISIBLE);
            tv_tian.setText("天");
            if (sel == 0) {
                tv_plus.setVisibility(View.VISIBLE);
                tv_plus.setText("第");
                tv_count.setText(GetTimeUtil.addDates(date, Dates) + "");
                tv_name.setText(name);
            } else if (sel == 1) {
                tv_plus.setVisibility(View.GONE);
                int counts = GetTimeUtil.addDates(Dates, date);
                if (counts > 0) {
                    tv_name.setText("距离" + name + "生日还有");
                    tv_count.setText(counts + "");
                } else {
                    tv_name.setText(name + "生日已过去");
                    tv_count.setText(GetTimeUtil.addDates(date, Dates) + "");
                }

            } else {
                tv_plus.setVisibility(View.GONE);
                int counts = GetTimeUtil.addDates(Dates, date);
                if (counts > 0) {
                    tv_name.setText(name + "仅剩");
                    tv_count.setText(counts + "");
                } else {
                    tv_name.setText(name + "已过去");
                    tv_count.setText(GetTimeUtil.addDates(date, Dates) + "");
                }
            }
            tv_date.setText(date);
            tv_bz.setText(bz);
            if (CompressedBitmap.fileExist(bg)) {
                Bitmap bitmap = BitmapFactory.decodeFile(bg);
                if (bitmap != null) {
                    bitmap = CompressedBitmap.Compressed(bitmap, 0.5f);
                    iv_bg.setImageBitmap(bitmap);
                }
            }
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_name.setText("");
        tv_count.setText("");
        tv_date.setText("");
        tv_bz.setText("");
        tv_plus.setText("");
        tv_tian.setText("");
        recyclerView.setAdapter(null);
        initList();
        initView();
    }
}
